package com.xsmart.order.services;

import com.baomidou.mybatisplus.plugins.Page;
import com.xsmart.common.constant.ResultConstant;
import com.xsmart.common.entity.RequestBean;
import com.xsmart.common.entity.ResponseBean;
import com.xsmart.order.dao.RecObjectDao;
import org.apache.ibatis.session.ResultContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.xml.ws.Response;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author DIYC
 */
@Service
public class RecObjectService {
    @Autowired
    RecObjectDao dao;

    /**
     * 分页查询
     * @param requestBean
     * @return
     */
    public ResponseBean queryRecObject(RequestBean requestBean){
        int currentPage = requestBean.getRequestParamInteger("currentPage", 1);
        int pageSize = requestBean.getRequestParamInteger("pageSize", 10);
        Integer seatId = requestBean.getRequestParamInteger("seat_id");
        Integer duId = requestBean.getRequestParamInteger("du_id");
        Map<String, Object> map = new HashMap<>();
        map.put("seat_id", seatId);
        map.put("du_id", duId);
        Page<Map<String, Object>> page = new Page<>(currentPage, pageSize);
        page.setRecords(dao.queryRecObject(page, map));
        ResponseBean responseBean = requestBean.getResponseBean();
        responseBean.addResultPageItem("recObjectList", page);
        responseBean.setResult(ResultConstant.SUCCESS);
        return responseBean;
    }

    /**
     * 更新
     * @param requestBean
     * @return
     */
    @Transactional
    public ResponseBean modRecObject(RequestBean requestBean){
        ResponseBean responseBean = requestBean.getResponseBean();
        Integer seatId = requestBean.getRequestParamInteger("seat_id");
        Integer duId = requestBean.getRequestParamInteger("du_id");
        Integer status = requestBean.getRequestParamInteger("status");
        String startTime = requestBean.getRequestParamString("start_time");
        String endTime = requestBean.getRequestParamString("end_time");
        String objTitle = requestBean.getRequestParamString("obj_title");
        String objImg = requestBean.getRequestParamString("obj_img");
        Integer priorityId = requestBean.getRequestParamInteger("priority_id");
        Map<String, Object> map = new HashMap<>();
        map.put("seat_id", seatId);
        map.put("du_id", duId);
        map.put("status", status);
        map.put("start_time", startTime);
        map.put("end_time", endTime);
        map.put("obj_title", objTitle);
        map.put("obj_img", objImg);
        map.put("priority_id", priorityId);
        try{
            int num = dao.modRecObject(map);
            int numInfo = dao.modRecObjectInfo(map);
            responseBean.setResult(ResultConstant.SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            responseBean.setResult(ResultConstant.ERROR);
        }
        return responseBean;
    }

    /**
     * 删除推荐对象及其推荐对象信息
     * @param requestBean
     * @return
     */
    @Transactional
    public ResponseBean delRecObject(RequestBean requestBean){
        ResponseBean responseBean = requestBean.getResponseBean();
        Integer priorityId = requestBean.getRequestParamInteger("priority_id");
        try{
            int num = dao.delRecObject(priorityId);
            int numInfo = dao.delRecObjectInfo(priorityId);
            responseBean.setResult(ResultConstant.SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            responseBean.setResult(ResultConstant.ERROR);
        }

        return responseBean;
    }

    /**
     * 新增推荐对象
     * @param requestBean
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseBean addRecObject(RequestBean requestBean){
        ResponseBean responseBean = requestBean.getResponseBean();
        Map<String,Object> recObjectMap = getParam(requestBean);
        Date addTime = new Date();
        recObjectMap.put("add_time",addTime);
        try {
            int num = dao.addRecObject(recObjectMap);
            List<Integer> idMap = dao.getId();
            Integer priorityId = idMap.get(0);
            recObjectMap.put("priority_id",priorityId);
            int num1 = dao.addRecObjectInfo(recObjectMap);
            responseBean.setResult(ResultConstant.SUCCESS);
        } catch (Exception e){
            e.printStackTrace();
            responseBean.setResult(ResultConstant.ERROR);
        }
        return responseBean;
    }

    /**
     * 获取参数
     * @param requestBean
     * @return
     */
    private Map<String, Object> getParam(RequestBean requestBean){
        String[][] params = new String[][]{{"seat_id", ""},
                {"user_id", ""},
                {"du_id", ""},
                {"obj_id", ""},
                {"obj_type", ""},
                {"status", ""},
                {"start_time", ""},
                {"obj_title", ""},
                {"obj_img", ""},
                {"end_time", ""}};
        Map<String, Object> recObjectMap = new HashMap<>();
        for (String [] param : params){
            Object value = requestBean.getRequestParam(param[0]);
            if(value == null){
                recObjectMap.put(param[0],param[1]);
            } else {
                recObjectMap.put(param[0],requestBean.getRequestParam(param[0]));
            }
        }
        return recObjectMap;
    }

}
